﻿using System;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Harvest.Api.Interfaces;
using System.Collections.Generic;

namespace Harvest.Api.Tests
{

    [TestClass]
    public class ClientsRepositoryTests
    {

        private static IRestRequestProvider _provider;
        private static Repository _repository;

        private ClientItem _mockClient;
        private Clients _mockClients;


        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            _provider = new RestRequestProvider("monotic", "thommy@mewes.org", "vader900");
            _repository = new Repository(_provider);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            _mockClient = new ClientItem()
            {
                Active = true,
                CacheVersion = "1234567",
                CreatedAt = new DateTime(2010, 01, 01, 12, 00, 00),
                Currency = "EUR",
                CurrencySymbol = "€",
                DefaultInvoiceTimeframe = "",
                Details = "",
                HighriseId = 1,
                Id = 1,
                Name = "Testclient1",
                UpdatedAt = new DateTime(2010, 01, 01, 12, 00, 00)
            };
            _mockClients = new Clients();
            _mockClients.ClientArray = new ClientItem[] { _mockClient };
        }

        [TestMethod]
        public void Test()
        {
            //var entities = _repository.Expenses.GetAll();

            //var entity1 = _repository.Expenses.GetById(547958);
            //var entity2 = _repository.Expenses.GetById(547959);
            //var entity3 = _repository.Expenses.GetById(547960);

            var x = _repository.TimeTrackings.GetForDay(DateTime.Now);
        }

        #region GetAll

        [TestMethod]
        public void Clients_01_GetAll()
        {
            var entities = _repository.Clients.GetAll();
            
            Assert.AreEqual(entities.Count, 1);
        }

        #endregion

        #region GetById

        [TestMethod]
        public void Clients_02_GetById()
        {
            var entities = _repository.Clients.GetAll();
            var entity = _repository.Clients.GetById(entities[0].Id.Value);

            Assert.IsNotNull(entity);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Clients_02_GetById_Fail01()
        {
            var entity = _repository.Clients.GetById(-100);
        }

        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void Clients_02_GetById_Fail02()
        {
            var entity = _repository.Clients.GetById(123456789);
        }

        #endregion

        #region AddNew

        [TestMethod]
        public void Clients_03_AddNew()
        {
            var newEntity = new Client() {Name = "UnitTestClient1", Details = "Address"};
            var entity = _repository.Clients.Add(newEntity);
            
            Assert.AreNotEqual(null, entity.Id);

            _repository.Clients.Remove(entity);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Clients_03_AddNew_Fail01()
        {
            var newEntity = new Client();
            var entity = _repository.Clients.Add(newEntity);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Clients_03_AddNew_Fail02()
        {
            var entity = _repository.Clients.Add(null);
        }

        #endregion

        #region Remove

        [TestMethod]
        public void Clients_04_Remove()
        {
            var newEntity = new Client() { Name = "UnitTestClient1", Details = "Address" };
            var entity = _repository.Clients.Add(newEntity);
            _repository.Clients.Remove(entity);
            var entities = _repository.Clients.GetAll();

            Assert.AreEqual(entities.Count, 1);
        }

        #endregion

        #region Deactivate

        [TestMethod]
        public void Clients_05_Deactivate()
        {
            var newEntity = new Client() { Name = "UnitTestClient1", Details = "Address" };
            var entity = _repository.Clients.Add(newEntity);
            _repository.Clients.Deactivate(entity);
            entity = _repository.Clients.GetById(entity.Id.Value);
            
            Assert.AreEqual(false, entity.Active);

            _repository.Clients.Remove(entity);
        }

        #endregion

        #region Update

        [TestMethod]
        public void Clients_06_Update()
        {
            var newEntity = new Client() { Name = "UnitTestClient1", Details = "Address" };
            var entity = _repository.Clients.Add(newEntity);
            entity.Name += "_Renamed";
            entity = _repository.Clients.Update(entity);

            Assert.AreEqual("UnitTestClient1_Renamed", entity.Name);

            _repository.Clients.Remove(entity);
        }

        #endregion

    }

}
